// house_pkg/pages/locate/index.ts
import qqmap from '../../../untils/getLocation'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    nearbyCommunities: [],
    currentLocation: '',
  },

  onLoad() {
    this.getCoordinate()
  },

  //获取当前坐标
  async getCoordinate() {
    const { latitude, longitude } = await wx.getLocation({
      type: 'gcj02',
    })
    this.getCurrent(latitude, longitude)
    this.getLocation(latitude, longitude)
  },

  //获取当前位置
  getCurrent(latitude, longitude) {
    const that = this
    qqmap.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude,
      },
      success(res) {
        console.log(res.result.address)
        that.setData({
          currentLocation: res.result.address,
        })
      },
      fail() {
        console.log('请求失败')
      },
    })
  },
  //获取附近社区
  getLocation(latitude, longitude) {
    const that = this
    qqmap.search({
      keyword: '住宅小区',
      page_size: 5,
      location: {
        latitude: latitude,
        longitude: longitude,
      },
      success: function (res) {
        // const { data } = res
        console.log('附近的社区有', res.data)
        that.setData({
          nearbyCommunities: res.data,
        })
      },
      fail: function () {
        console.log('查询失败')
      },
    })
  },
  //重新定位
  async reLocated() {
    const { latitude, longitude } = await wx.chooseLocation({})
    console.log(latitude, longitude)
    this.getCurrent(latitude, longitude)
    this.getLocation(latitude, longitude)
  },
  //页面跳转
})
